package floappcontest.core.beans;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

import floappcontest.core.beans.interfaces.PersistentObject;


@Entity
@Table( name = "faq" )
@OnDelete(action=OnDeleteAction.CASCADE)
public class Faq implements PersistentObject, Comparable<Faq>, Serializable {

	private static final long serialVersionUID = 1L;
	private String answer;
	private String question;
	private Contest contest;


	public Faq() {
		// Default empty constructor
		answer = new String();
		question = new String();
	}

	public Faq( String question, String answer ) {
		setAnswer( answer );
		setQuestion(question);
	}
	
	@OneToOne(fetch=FetchType.LAZY) //only OneToOne
	@JoinColumn(name="faq_fk") //, insertable=true, updatable=false 
	public Contest getContest() {
		return this.contest;
	}
	
	public void setContest( Contest contest ) {
		this.contest = contest;
	}

	@Column( name = "answer", length = 400, nullable = false )
	public String getAnswer() {
		return this.answer;
	}

	public void setAnswer( String answer ) {
		this.answer = answer;
	}

	@Id
	@Column( name = "question", length = 255, nullable = false )
	public String getQuestion() {
		return question;
	}

	public void setQuestion(String question) {
		this.question = question;
	}

	@Override
	public boolean equals( Object obj ) {
		if ( obj instanceof Faq ) {
			Faq f = (Faq) obj;
			return (f.getQuestion().equalsIgnoreCase(this.question));
		}
		return false;
	}


	public int compareTo( Faq f ) {
		return getQuestion().compareToIgnoreCase(f.getQuestion());
	}	
	
}